<?php

namespace app\api\controller;

use app\common\controller\Api;
use addons\shopro\model\Goods as GoodsModel;
use app\common\model\GoodsCategory;

/**
 * 商品接口
 */
class Goods extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 商品列表
     */
    public function index()
    {
        $params = $this->request->param();

        if (!empty($params['postage'])) {
            $category = GoodsCategory::where('name', 'like', '付邮轻享%')->find();
            if ($category) {
                $params['category_id'] = $category['id'];
            }
        }
        if (empty($params['category_id'])) {
            $params['category_id'] = 0;
        }

        $goods = GoodsModel::getGoodsList($params);

        $this->success('', $goods);
    }

    /**
     * 商品详情
     * @param $ids
     *
     * @return void
     */
    public function detail($ids = null)
    {
        if (!$ids) {
            $ids = $this->request->get('id');
        }
        if (!$ids) {
            $this->error('请求异常，请刷新重试');
        }

        try {
            $detail = GoodsModel::getGoodsDetail($ids);
        } catch(\Exception $e) {
            $this->error($e->getMessage());
        }

        $this->success('请求成功', $detail);
    }
}
